package com.zhudeshuai.zhudemolist.activity;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.zhu.zhuCore.utils.LogUtil;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.util.NotificationManagerUtil;
import com.zhudeshuai.zhudemolist.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Rxjava入门
 *
 * @author zhudeshuai
 * @since 16/9/20
 */
public class RxJavaActivity extends AppCompatActivity {

    private static final java.lang.String TAG = "RxJava";

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                int test = intent.getIntExtra("test", -1);
                LogUtil.d(TAG, "接收到的值:" + test);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        rxJavaDemo6();
        IntentFilter filter = new IntentFilter("com.zhu");
        registerReceiver(mBroadcastReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    /**
     * 测试
     *
     * @param view
     */
    public void showTest(View view) {
        for (int i = 0; i < 5; i++) {
            Intent intent = new Intent();
            intent.setAction("com.zhu");
            intent.putExtra("test", i);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationManagerUtil.showNotification(this, pendingIntent, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, "测试" + i, "测试" + i);
        }
    }

    private void initPageView() {
        HashMap<String, Fragment> hashMap = new HashMap<>();
        hashMap.put("测试1", new Fragment());
        hashMap.put("测试2", new Fragment());
        hashMap.put("测试3", new Fragment());
    }


    /**
     * flatMap使用
     */
    private void rxJavaDemo6() {
        List<Student> students = new ArrayList<>();
        {
            //准备数据
            Student a = new Student();
            a.name = "张三";
            a.courses.add(new Course("语文"));
            a.courses.add(new Course("数学"));
            Student b = new Student();
            b.name = "李四";
            b.courses.add(new Course("政治"));
            b.courses.add(new Course("地理"));
            students.add(a);
            students.add(b);
        }

        Observable.from(students)
                .flatMap(new Func1<Student, Observable<Course>>() {
                    @Override
                    public Observable<Course> call(Student student) {
                        return Observable.from(student.courses);
                    }
                })
                .subscribe(new Action1<Course>() {
                    @Override
                    public void call(Course course) {
                        LogUtil.d(TAG, course.name);
                    }
                });


    }


    /**
     * 学生
     */
    class Student {
        public String name;
        public List<Course> courses = new ArrayList<>();
    }

    /**
     * 课程
     */
    class Course {
        public Course(String name) {
            this.name = name;
        }

        public String name;
    }

    /**
     * map使用
     */
    private void rxJavaDemo5() {
        Observable.just(1, 2, 3, 4, 5)
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return "数据转化:" + integer;
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        LogUtil.d(TAG, s);
                    }
                });
    }


    /**
     * 线程切换
     */
    private void rxJavaDemo4() {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                String name = Thread.currentThread().getName();
                LogUtil.d(TAG, name + "---");
                subscriber.onNext("a");
                subscriber.onNext("b");
                subscriber.onNext("c");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String o) {
                        String name = Thread.currentThread().getName();
                        LogUtil.d(TAG, name + "---" + o);
                    }
                });
    }


    /**
     * 不完整定义回调
     */
    private void rxJavaDemo3() {

        Observable<String> observable = Observable.just("a", "b", "c");

        Action1<String> onNext = new Action1<String>() {
            @Override
            public void call(String o) {
                System.out.println(o);
            }
        };

        Action1<Throwable> onError = new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                System.out.println(throwable.toString());
            }
        };

        Action0 onCompletedAction = new Action0() {
            @Override
            public void call() {
                System.out.println("onCompletedAction");
            }
        };

        observable.subscribe(onNext, onError, onCompletedAction);

    }

    private void rxJavaDemo2() {
        List<String> data = new ArrayList<>();
        data.add("aaa");
        data.add("bbb");
        data.add("ccc");
        data.add("ddd");
        Observable.from(data).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted...");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println(e.toString());
            }

            @Override
            public void onNext(String o) {
                System.out.println(o);
            }
        });
    }


    private void rxJavaDemo1() {
        Observable<String> observable = Observable.just("a", "b", "c");
        observable.subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("队列结束");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("e:" + e);
            }

            @Override
            public void onNext(String s) {
                ToastUtil.showSnackBar(RxJavaActivity.this, s);
                System.out.println("参数为:" + s);
            }
        });
    }
}
